<template>
  <div>
    <van-address-edit
      :area-list="areaList"
      :address-info="myobj"
      :show-postal="false"
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-detail="onChangeDetail"
    />
  </div>
</template>

<script>
import { areaList } from '@vant/area-data';
import { Toast } from 'vant';
import {mapMutations} from "vuex"

export default {
  data() {
    return {
      areaList,
      searchResult: [],
      myobj:{}
    };
  },
  created () {
  console.log(this.$route.query);
      this.myobj = this.$route.query
      this.myobj.addressDetail = this.$route.query.address
  },
//   mounted () {
//       this.onChangeDetail()
//   },
  methods: {
    ...mapMutations(['addressfn',"addresseditfn","delfn"]),
    onSave(content) { // 判断是新增还是编辑

       console.log(content);
       let address = content.city + content.county + content.addressDetail
       let obj = {
           name:content.name,
           tel:content.tel,
           address,
           id: new Date ()  * 1,
           index:this.$route.query.index
       }
       if (this.$route.query.isadd  === "编辑") { // 新增没有传参，如果有参数说明是编辑，没有编辑说明是新增
          
            this.addresseditfn(obj)
       } else  if (this.$route.query.isadd  === "新增" ){
           
            this.addressfn(obj)
       }
     
       this.$router.back()

    //    console.log(obj,"obj")

    },
    onDelete() {
      Toast('delete');
      this.delfn(this.$route.query.index)
      this.$router.back()
    },
    onChangeDetail(val) {
      
      if (!val) {
        this.searchResult = [
          {
            name: '八维学校',
            address: '北京市顺义区',
          },
        ];

      } 
      else {
        this.searchResult = [];
      }
    },
  },
};
</script>

<style>
</style>